package com.clothes.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by llf on 2017/12/8.
 */

public class StringUtils {

    public static String replace (String str, String oldStr, String newStr){
        if(str!=null && !str.equals(""))
        return str.replace(oldStr,newStr);
        return "";
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDate(Date date) {
        if(date == null)
            date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     *
     */

    public static int getNumStr(String num){
        if(num.equals("")) return 0;
        return Integer.parseInt(num);
    }
    public static double getDoubleStr(String num){
        if(num.equals("")) return 0;
        return Double.parseDouble(num);
    }

    //获取list字符串
    public static String getStrByList(List<String> list){
        String res = "";
        for (String ss : list){
            res += ss+",";
        }
        if(res.length()>0)
            res = res.substring(0,res.lastIndexOf(","));
        return res;
    }

    //根据字符串获取list
    public static List<String> getListByStr(String res,String type){
        List<String> list = new ArrayList<>();
        if(res == null || res.equals(""))
            return list;
        String[] ary = res.split(type);
        for (int i = 0; i < ary.length; i++) {
            list.add(ary[i]);
        }
        return list;
    }
}
